package sealte.hvpms.servlet.splitpage;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sealte.hvpms.dao.MessageDAO;
import sealte.hvpms.dao.impl.MessageDAOImpl;
import sealte.hvpms.pojo.Admin;
import sealte.hvpms.pojo.Message;
import sealte.hvpms.pojo.UserCreate;

import com.google.gson.Gson;

public class GetMessagesAll extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control","no-cache");
		UserCreate uc = (UserCreate)request.getSession().getAttribute("UserCreate");
		Admin admin = (Admin)request.getSession().getAttribute("admin");
		PrintWriter out = response.getWriter();
		if(admin!=null||uc!=null){
			String kw = (String)request.getParameter("keyWord");
			String currentPage = (String)request.getParameter("currentPage");
			int cp = Integer.parseInt(currentPage);
			String lineSize = (String)request.getParameter("lineSize");
			int ls = Integer.parseInt(lineSize);
			MessageDAO mb = new MessageDAOImpl();
			List<Message> all = mb.listAll(cp, ls, kw);
			Gson gson = new Gson();
			String json = gson.toJson(all);
			out.write(json);
		}else{
			out.println("谢绝跨路径访问！");
		}
	}
}
